package com.larsgard.gedviewer.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.larsgard.gedviewer.client.event.EventBus;
import com.larsgard.gedviewer.client.event.LoggedInEvent;
import com.larsgard.gedviewer.client.event.SearchResultEvent;
import com.larsgard.gedviewer.client.service.GedUserService;
import com.larsgard.gedviewer.client.service.GedUserServiceAsync;
import com.larsgard.gedviewer.client.ui.newlayout.components.PersonDetailsPanel;
import com.larsgard.gedviewer.client.ui.newlayout.components.SearchBox;
import com.larsgard.gedviewer.client.ui.newlayout.components.SearchResultPanel;
import com.larsgard.gedviewer.client.ui.newlayout.content.UsersAdmin;
import com.larsgard.gedviewer.shared.ajax.IsLoggedInResponse;
import com.larsgard.gedviewer.shared.ajax.SearchResult;

public class MainUi extends Composite implements SearchResultEvent.SearchResultHandler,
	LoggedInEvent.LoginHandler {

    private GedUserServiceAsync userserService = GWT.create(GedUserService.class);

    private static MainUiUiBinder uiBinder = GWT.create(MainUiUiBinder.class);

    interface MainUiUiBinder extends UiBinder<Widget, MainUi> {
    }

    @UiField
    HorizontalPanel content, statusBar, header;

    @UiField
    static Image waiting = new Image("gfx/waiting.gif");

    @UiField
    Label status, mySettingsLabel;

    public MainUi() {
	initWidget(uiBinder.createAndBindUi(this));
	EventBus.getInstance().addHandler(SearchResultEvent.TYPE, this);
	EventBus.getInstance().addHandler(LoggedInEvent.TYPE, this);

	setWaiting(false);
	checkPasswordAndLogin();
    }

    @Override
    public void onResultLoaded(SearchResultEvent p) {
	SearchResult result = p.getSearchResult();
	String msg;
	switch (result.getType()) {
	case DETAIL_FOR_PERSON:
	    msg = "Showing details for " + result.getPersonDetails()[0].getMainPerson().toString();
	    break;
	case LIST_OF_PERSONS:
	    msg = "Showing result for search, result contains " + result.getPersons().size()
		    + " persons.";
	    break;
	default:
	    msg = "Showing result.";
	    break;
	}
	status.setText(msg);
    }

    @Override
    public void onLoginCheckResult(IsLoggedInResponse response) {
	setWaiting(false);
	String html = "";
	if (response.isLoggedIn()) {
	    status.setText("Logged in.");
	    mySettingsLabel.setText("Settings");
	    html += "<a href=\"" + response.getLoginUrl() + "\">Logout</a>";
	} else {
	    status.setText("Not logged in.");
	    html = "<a href=\"" + response.getLoginUrl() + "\">Login</a>";
	}
	String password = Window.Location.getParameter("pwd");
	if (password != null) {
	    showUserTree(password);
	} else {
	    content.clear();
	    content.add(new VisitorContent());
	}
	statusBar.add(new HTML(html));
    }

    @UiHandler("mySettingsLabel")
    public void showMySettings(ClickEvent event) {
	content.clear();
	content.add(new UsersAdmin());
    }

    private void showUserTree(String password) {
	content.clear();
	header.add(new SearchBox());
	content.add(new SearchResultPanel());
	content.add(new PersonDetailsPanel());
    }

    private void checkPasswordAndLogin() {
    }

    public static void setWaiting(boolean b) {
	waiting.setUrl("gfx/waiting.gif");
	waiting.setSize("50px", "50px");
	waiting.setVisible(b);
    }

}
